package mgoasync

import (
	"go.mongodb.org/mongo-driver/bson"
)

// 增量(差异)更新模式序列化方法，生成用于更新 MongoDB 的[改变|新增]字段 `$set`` 和[删除]字段 `$unset`
func IncrSerializer(up map[string]interface{}, del map[string]bool) (interface{}, error) {
	var serialized bson.D
	if len(up) != 0 {
		s, err := bson.Marshal(up)
		if err != nil {
			return nil, err
		}
		serialized = append(serialized, bson.E{Key: "$set", Value: bson.RawValue{Type: bson.TypeEmbeddedDocument, Value: s}})
	}
	if len(del) > 0 {
		s, err := bson.Marshal(del)
		if err != nil {
			return nil, err
		}
		serialized = append(serialized, bson.E{Key: "$unset", Value: bson.RawValue{Type: bson.TypeEmbeddedDocument, Value: s}})
	}
	return serialized, nil
}
